package com.huanmeiqi.demo.quartz.demo02;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author cq.Wang
 * @date 2018/6/13 17:20
 * @description
 */
@RestController
@RequestMapping(value = "jobs")
public class JobController {

    @Autowired
    private QuartzJobManager quartzJobManager;

    /**
     * 添加Job
     * <p>
     * {
     * "jobName":"addJob",
     * "jobGroupName":"addJobGroup",
     * "triggerName":"addJobTrigger",
     * "triggerGroupName":"addJobTriggerGroup",
     * "jobClassName":"com.huanmeiqi.demo.quartz.demo02.AddJob",
     * "cronExpression":"0/1 * * * * ?"
     * }
     *
     * @param addJobParam
     * @return
     * @throws ClassNotFoundException
     */
    @PostMapping
    public Date addJob(@RequestBody AddJobParam addJobParam) throws ClassNotFoundException {
        String jobName = addJobParam.getJobName();
        String jobGroupName = addJobParam.getJobGroupName();
        String triggerName = addJobParam.getTriggerName();
        String triggerGroupName = addJobParam.getTriggerGroupName();
        Class<?> aClass = Class.forName(addJobParam.getJobClassName());
        String cronExpression = addJobParam.getCronExpression();
        return quartzJobManager.addJob(jobName, jobGroupName, triggerName, triggerGroupName, aClass, cronExpression);
    }

//    @DeleteMapping(value = "{jobName}")
//    public boolean deleteJob(@PathVariable("jobName") String jobName) {
//        System.err.println("------>" + jobName);
//        return quartzJobManager.deleteJob(jobName);
//    }
}
